package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCrossUnionShipData;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCurSpeedInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/13 10:23
 */
public class SeacraftSouthSeaCrossUnionShipDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaCrossUnionShipData> {
    public static String getSeacraftSouthSeaCrossUnionShipDataSql = "select * from `t_c_activity_seacraft_southsea_union_ship` where `activityId`=?";
    public Map<String, SeacraftSouthSeaCrossUnionShipData> getSeacraftSouthSeaCrossUnionShipDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, SeacraftSouthSeaCrossUnionShipData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaCrossUnionShipDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaCrossUnionShipData item = parse(rs);
                map.put(item.getUnionId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSeacraftSouthSeaCrossUnionShipDataSql = "update `t_c_activity_seacraft_southsea_union_ship` set totalDurable=?,remainDurable=?,fixRefreshTime=?,speedRecordList=?,distance=?,refreshTime=?,endTime=? where activityId=? and unionId=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaCrossUnionShipData seacraftSouthSeaCrossUnionShipData) {
        seacraftSouthSeaCrossUnionShipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getTotalDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getRemainDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getFixRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionShipData.getSpeedRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionShipData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUnionShipData.getUnionId()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaCrossUnionShipDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaCrossUnionShipDataSql = "insert into `t_c_activity_seacraft_southsea_union_ship`(activityId,unionId,totalDurable,remainDurable,fixRefreshTime,speedRecordList,distance,refreshTime,endTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaCrossUnionShipData seacraftSouthSeaCrossUnionShipData) {
        seacraftSouthSeaCrossUnionShipData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionShipData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUnionShipData.getUnionId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getTotalDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getRemainDurable()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getFixRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionShipData.getSpeedRecordList())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getDistance()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftSouthSeaCrossUnionShipData.getEndTime()));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaCrossUnionShipDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaCrossUnionShipData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaCrossUnionShipData seacraftSouthSeaCrossUnionShipData = new SeacraftSouthSeaCrossUnionShipData();
        seacraftSouthSeaCrossUnionShipData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaCrossUnionShipData.setUnionId(rs.getString("unionId"));
        seacraftSouthSeaCrossUnionShipData.setTotalDurable(rs.getLong("totalDurable"));
        seacraftSouthSeaCrossUnionShipData.setRemainDurable(rs.getLong("remainDurable"));
        seacraftSouthSeaCrossUnionShipData.setFixRefreshTime(rs.getLong("fixRefreshTime"));
        seacraftSouthSeaCrossUnionShipData.setSpeedRecordList(JSON.parseObject(rs.getString("speedRecordList"), new TypeReference<CopyOnWriteArrayList<SeacraftSouthSeaCurSpeedInfo>>() {}));
        seacraftSouthSeaCrossUnionShipData.setDistance(rs.getLong("distance"));
        seacraftSouthSeaCrossUnionShipData.setRefreshTime(rs.getLong("refreshTime"));
        seacraftSouthSeaCrossUnionShipData.setEndTime(rs.getLong("endTime"));

        return seacraftSouthSeaCrossUnionShipData;
    }
}
